package com.hnqcgczy.controller;

import com.hnqcgczy.service.IShopCartService;
import com.hnqcgczy.service.impl.ShopCartServiceImpl;
import com.hnqcgczy.vo.ShopCartVo;
import com.hnqcgczy.vo.TShopcart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/***
 * 购物车的增删改查
 */
@WebServlet("/shopCart")
public class ShopCartServlet extends HttpServlet {
    IShopCartService cartService=new ShopCartServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从session中获取用户编号
        String tuId=(String)req.getSession().getAttribute("tuId");
        System.out.println(tuId);
        if(tuId==null){
            //没有从session中获取登录信息， 强制跳转到登录页
            resp.sendRedirect("/login.jsp");
            return;
        }
        //如果是新增流程， 完成新增操作
        String m=req.getParameter("m");
        if("add".equals(m)){
            addCart(req,resp);
        }else if("del".equals(m)){
            //执行删除方法
            delCart(req,resp);
        }else if("remove".equals(m)){
            removeCart(req,resp);
        }else if("update".equals(m)){
            updateCart(req,resp);
        }

        //根据用户编号， 查询所有购物车数据
        List<ShopCartVo> cartlist=cartService.getShopCartList(tuId);
        System.out.println(cartlist.size());
        //将查询的数据， 存储于request作用域
        req.setAttribute("cartlist",cartlist);
        //转发到购物车列表的jsp
        req.getRequestDispatcher("/shopCart.jsp").forward(req,resp);
    }

    /***
     * 添加购物车
     * @param req
     * @param resp
     */
    private void addCart(HttpServletRequest req, HttpServletResponse resp){
        //从参数中获取商品编号
        String tpId=req.getParameter("tpId");
        System.out.println(tpId);
        //调用服务层接口， 录入到数据库
        TShopcart cart=new TShopcart();
        String tuId=(String)req.getSession().getAttribute("tuId");
        cart.setTuId(tuId);
        cart.setTpId(Long.parseLong(tpId));
        cart.setTsCount(1);
        cartService=new ShopCartServiceImpl();
        cartService.addShopCart(cart);
    }

    /***
     * 删除购物车
     * @param req
     * @param resp
     */
    private void delCart(HttpServletRequest req, HttpServletResponse resp){
        //获取删除的购物车主键
        String tsId=req.getParameter("tsId");
        //执行删除方法
        cartService.deleteShopCart(Integer.parseInt(tsId));
    }

    /***
     * 清空购物车
     */
    public void removeCart(HttpServletRequest req, HttpServletResponse resp){
        //获取登录用户编号
        String tuId=(String)req.getSession().getAttribute("tuId");
        //调用服务层删除购物车
        cartService.removeShopCart(tuId);
    }

    /***
     * 完成购物车数量修改 + -
     * @param req
     * @param resp
     */
    public void updateCart(HttpServletRequest req, HttpServletResponse resp) {
        // 获取购物车编号
        int tsId=Integer.parseInt(req.getParameter("tsId"));
        //获取购物车修改后的数量
        int num=Integer.parseInt(req.getParameter("num"));
        TShopcart cart=new TShopcart();
        //设置购物车编号
        cart.setTsId(tsId);
        //设置购物车修改后的数量
        cart.setTsCount(num);
        //调用服务层
        cartService.updateShopCart(cart);

    }
}
